module diansha {
    /**
     * 资源管理工具
     */
    export class res_manage {
        private static ossurl = 'https://wkk-1258271423.cos.ap-beijing.myqcloud.com/';
        private static D: res_manage = null;
        private big_arr: Array<res_obj> = new Array<res_obj>();
        private one;
        private namestr;
        private name;
        constructor() {


        }


        /**
               * 获取资源
               * 先从本地找、有则返回没有则网络处下载并存储到本地
               * //用的时候再取，无需游戏一开始加载 
               */
        public async getres(name: string) {
            this.name = name;
            this.namestr = name.split("_")[1];

            if (RES.hasRes(name)) {
                //如果本地已缓存中存在
                return RES.getRes(name)
            } else {
                let non = this.checek(name);
                if (non != null) {
                    //如果刚刚手动缓存过
                    return non
                } else {
                    //都没有，则下载 ,存起来
                    name = name.replace("_", ".");
                    await RES.getResByUrl(diansha.res_manage.ossurl + name, this.loadcomplete, this);
                    return this.one;
                }
            }


        }

        private loadcomplete(event: any) {
            if (this.namestr == "png") {
                var img: egret.Texture = <egret.Texture>event;
                var bitmap: egret.Bitmap = new egret.Bitmap(img);
                bitmap.cacheAsBitmap = true
                this.one = bitmap;
                this.big_arr.push(new diansha.res_obj(this.name, this.one));
                return this.one;
            }


        }
        /**
         * 判断是否手动缓存过
         * null:没有手动存过
         * any：之前手动存的
         */
        private checek(name: string): any {
            let s = null
            this.big_arr.forEach((v, i) => {
                if (v.name == name) {
                    return v;
                }
            })
            return s;
        }
        public static getinstance(): res_manage {
            if (this.D == null) {
                this.D = new res_manage();
            }
            return this.D;
        }

    }
}